// @import
import GameScene from "./GameScene";
import HeroCtrl, { g_oHeroCtrlMap } from "../ctrl/HeroCtrl";
import MsgBus from "../comm/network/MsgBus";
import UserData from "../data/UserData";

// @import
import mod_gameMsgProtocol = require("../msg/mod_gameMsgProtocol");

/**
 * 监听操作事件
 * 
 * @param SELF this 指针
 * @return {void}
 */
export function __UI__listenOperationAreaEvent(SELF: GameScene): void {
    if (!SELF) {
        return;
    }

    let oOperationArea: cc.Node = cc.find("/Canvas/UI/Holder_OperationArea");

    // 有鼠标的情况
    oOperationArea.on(cc.Node.EventType.MOUSE_UP, (oEvent: any) => {
        if (cc.sys.os != cc.sys.OS_LINUX &&
            cc.sys.os != cc.sys.OS_OSX &&
            cc.sys.os != cc.sys.OS_WINDOWS) {
            // 如果不是 PC 操作系统,
            return;
        }

        switch (oEvent.getButton()) {
            case 0: {
                // 执行攻击
                __doAttk();
                break;
            }

            case 2: {
                let oToWorldPos: cc.Vec2 = cc.v2(0, 0);
                // 将鼠标转换为世界坐标
                SELF._oMainCameraNode.getComponent(cc.Camera).getScreenToWorldPoint(cc.v2(
                    oEvent.getLocationX(),
                    oEvent.getLocationY()
                ), oToWorldPos);

                // 执行移动
                __doMoveTo(oToWorldPos);
                break;
            }

            default:
                break;
        }
    });

    // 当发生触屏时
    oOperationArea.on(cc.Node.EventType.TOUCH_START, (oEvent: any) => {
        if (cc.sys.os != cc.sys.OS_ANDROID &&
            cc.sys.os != cc.sys.OS_IOS) {
            // 如果不是手机操作系统,
            return;
        }

        let oToWorldPos: cc.Vec2 = cc.v2(0, 0);
        // 将鼠标转换为世界坐标
        SELF._oMainCameraNode.getComponent(cc.Camera).getScreenToWorldPoint(cc.v2(
            oEvent.getLocationX(),
            oEvent.getLocationY()
        ), oToWorldPos);

        // 执行移动
        __doMoveTo(oToWorldPos);
    });
}

/**
 * 执行攻击
 */
function __doAttk(): void {
    let oHeroCtrl: HeroCtrl = g_oHeroCtrlMap.get(UserData.getMyData()._nUserId);

    if (null == oHeroCtrl) {
        return;
    }

    // 执行攻击
    let out_oTargetUserIdArray: Array<number> = [];
    oHeroCtrl.doAttk(out_oTargetUserIdArray);

    if (out_oTargetUserIdArray.length <= 0) {
        return;
    }

    for (let nTargetUserId of out_oTargetUserIdArray) {
        if (nTargetUserId <= 0) {
            continue;
        }

        MsgBus.getInstance().sendMsg(
            mod_gameMsgProtocol.msg.MsgCode.USER_ATTK_CMD,
            mod_gameMsgProtocol.msg.UserAttkCmd.create({
                targetUserId: nTargetUserId,
            }),
        );
    }
}

/**
 * 执行移动
 * 
 * @param oToWorldPos 世界位置
 */
function __doMoveTo(oToWorldPos: cc.Vec2): void {
    if (null == oToWorldPos) {
        return;
    }

    let oHeroCtrl: HeroCtrl = g_oHeroCtrlMap.get(UserData.getMyData()._nUserId);

    if (null == oHeroCtrl) {
        return;
    }

    let oFromWorldPos: cc.Vec2 = oHeroCtrl.node.convertToWorldSpaceAR(cc.Vec2.ZERO_R);
    oHeroCtrl.moveTo(oToWorldPos);

    MsgBus.getInstance().sendMsg(
        mod_gameMsgProtocol.msg.MsgCode.USER_MOVE_TO_CMD,
        mod_gameMsgProtocol.msg.UserMoveToCmd.create({
            moveFromPosX: oFromWorldPos.x,
            moveFromPosY: oFromWorldPos.y,
            moveToPosX: oToWorldPos.x,
            moveToPosY: oToWorldPos.y,
        }),
    );
}
